package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
public class test {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Test
    public void testString(){
        redisTemplate.opsForValue().set("name","zhangsan");
        redisTemplate.opsForValue().set("age","18");

        System.out.println(redisTemplate.opsForValue().get("name"));
        System.out.println(redisTemplate.opsForValue().get("age"));


    }



    @Test
    public void testHash(){
        //新增
        redisTemplate.opsForHash().put("user","name","zhangsan");
        redisTemplate.opsForHash().put("user","age","12");
        //获取
        System.out.println(redisTemplate.opsForHash().get("user", "name"));
        System.out.println(redisTemplate.opsForHash().get("user", "age"));
        //获取所有key
        System.out.println(redisTemplate.opsForHash().keys("user"));
        //获取所有value
        System.out.println(redisTemplate.opsForHash().values("user"));

        //删除小key
        System.out.println(redisTemplate.opsForHash().delete("user","name"));

        Long user = redisTemplate.opsForHash().delete("user");
        System.out.println(user);


    }




    @Test
    public void testList(){

        //插入
        redisTemplate.opsForList().leftPush("list","1");
        redisTemplate.opsForList().leftPush("list","2");
        redisTemplate.opsForList().leftPush("list","3");
        //获取
        System.out.println(redisTemplate.opsForList().range("list", 0, -1));

        System.out.println(redisTemplate.opsForList().size("list"));

        System.out.println(redisTemplate.opsForList().leftPop("list"));
        System.out.println(redisTemplate.opsForList().rightPop("list"));
        System.out.println(redisTemplate.opsForList().range("list", 0, -1));


    }



    @Test
    public void testSet(){
        redisTemplate.opsForSet().add("set","1","2","3");
        redisTemplate.opsForSet().add("set1","1","3","5");


        System.out.println(redisTemplate.opsForSet().members("set"));
        System.out.println(redisTemplate.opsForSet().members("set1"));


        System.out.println(redisTemplate.opsForSet().intersect("set","set1"));

        System.out.println(redisTemplate.opsForSet().union("set","set1"));

        System.out.println(redisTemplate.opsForSet().difference("set","set1"));

    }


    @Test
    public void testZSet(){
        //插入
        redisTemplate.opsForZSet().add("zset","张三",1);
        redisTemplate.opsForZSet().add("zset","李四",3);
        redisTemplate.opsForZSet().add("zset","王五",6);
        redisTemplate.opsForZSet().add("zset","干干净净的",10);
        // 获取
        System.out.println(redisTemplate.opsForZSet().range("zset", 0, -1));
        //增值
        redisTemplate.opsForZSet().incrementScore("zset","张三",4);
        System.out.println(redisTemplate.opsForZSet().range("zset", 0, -1));

        System.out.println(redisTemplate.opsForZSet().remove("zset","张三"));
    }


    @Test
    public void test(){
        //所有key
        System.out.println(redisTemplate.keys("*"));
        //包括
        System.out.println(redisTemplate.hasKey("zset"));
        //返回类型
        System.out.println(redisTemplate.type("zset"));
        //删除
        System.out.println(redisTemplate.delete("set"));
    }



}
